﻿// 功能：输入法帮助类
// 描述：
// 作者：温伟鹏
// 日期：2010/5/18 16:42:05

using System;
using System.Runtime.InteropServices;
using MeizuSDK.Drawing;
using System.Drawing;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 输入法帮助类
    /// </summary>
    public class SipHelper
    {
        #region DllImport

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool GetSipModeEx(ref uint dwMode);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern void MzCloseSipEx();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern uint MzGetA4SipMessageEx();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern void MzGetVisibleDesktopRectEx(ref RECT rect);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool MzIsSipOpenEx();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern void MzOpenSipFunc(uint dwMode, uint dwSipOffset, ref RECT rect);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern void MzOpenSipExFunc(uint dwMode, uint dwSipOffset, [MarshalAs(UnmanagedType.LPTStr)]string pszReturnName, ref RECT rect);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool SendCopyInfoToA4SIPEx();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern bool SetSipWindowPosEx(uint dwHeight);

        #endregion

        #region 常量
        /// <summary>
        /// 获取默认的输入法底部偏移值
        /// </summary>
        public const uint DefaultSipOffset = 0xffffffff;
        #endregion

        #region 静态属性
        /// <summary>
        /// 获取一个值，指示输入面板是否打开
        /// </summary>
        public static bool IsSipOpen
        {
            get
            {
                return MzIsSipOpenEx();
            }
        }
        /// <summary>
        /// 获取输入法当前键盘状态。
        /// </summary>
        public static SipMode CurrentSipMode
        {
            get
            {
                uint mode = 0;
                bool result = GetSipModeEx(ref mode);
                // FALSE表示返回错误（非A4输入法）
                if (result)
                {
                    return (SipMode)mode;
                }
                else
                {
                    return SipMode.None;
                }
            }
        }
        #endregion

        #region 静态函数
        /// <summary>
        /// 关闭输入法面板
        /// </summary>
        public static void CloseSip()
        {
            MzCloseSipEx();
        }
        /// <summary>
        /// 打开输入面板
        /// </summary>
        /// <param name="mode">输入法键盘输入模式</param>
        /// <returns></returns>
        public static Rectangle OpenSip(SipMode mode)
        {
            return OpenSip(mode, DefaultSipOffset);
        }
        /// <summary>
        /// 打开输入面板
        /// </summary>
        /// <param name="mode">输入法键盘输入模式</param>
        /// <param name="offset">输入法面板底部偏移值，0xffffffff代表系统默认，即竖屏为68，横屏为0</param>
        /// <returns></returns>
        public static Rectangle OpenSip(SipMode mode, uint offset)
        {
            RECT rect = new RECT();

            MzOpenSipFunc((uint)mode, offset, ref rect);

            return rect.ToRectangle();
        }
        /// <summary>
        /// 设置输入法面板底部偏移值
        /// </summary>
        /// <param name="height">输入法面板底部偏移值</param>
        /// <returns></returns>
        public static bool SetSipWindowPos(uint height)
        {
            return SetSipWindowPosEx(height);
        }
        /// <summary>
        /// 发送复制的消息给输入法控件
        /// </summary>
        /// <returns></returns>
        public static bool SendCopyInfoToA4Sip()
        {
            return SendCopyInfoToA4SIPEx();
        }
        /// <summary>
        /// 获取输入法控件发送给编辑控件的消息
        /// </summary>
        /// <returns></returns>
        public static A4SipMessage GetA4SipMessage()
        {
            uint msg = MzGetA4SipMessageEx();

            return (A4SipMessage)msg;
        }
        /// <summary>
        /// 获取输入框面板上方有效区域的矩形
        /// </summary>
        /// <returns></returns>
        public static Rectangle GetVisibleDesktopArea()
        {
            RECT rect = new RECT();

            MzGetVisibleDesktopRectEx(ref rect);

            return rect.ToRectangle();
        }
        #endregion
    }
    /// <summary>
    /// 输入法键盘输入方式
    /// </summary>
    public enum SipMode : uint
    {
        /// <summary>
        /// 维持输入法现有模式
        /// </summary>
        Keep = 0,
        /// <summary>
        /// 切换为拼音输入法
        /// </summary>
        GEL_PY,
        /// <summary>
        /// 切换标准的英文键盘输入
        /// </summary>
        GEL_Letter,
        /// <summary>
        /// 切换为邮件键盘英文方式
        /// </summary>
        Mail_Letter,
        /// <summary>
        /// 切换为网页键盘英文方式
        /// </summary>
        Web_Letter,
        /// <summary>
        /// 切换为数字键盘方式
        /// </summary>
        Address_123,
        /// <summary>
        /// 切换为联系人英文键盘方式
        /// </summary>
        Address_Letter,
        /// <summary>
        /// 切换为搜索英文键盘方式
        /// </summary>
        Search_Letter,
        /// <summary>
        /// 切换智能英文键盘输入
        /// </summary>
        GEL_English,
        /// <summary>
        /// 数字键盘
        /// </summary>
        Digit,
        /// <summary>
        /// 扩展数字键盘
        /// </summary>
        DigitEx,
        /// <summary>
        /// 无输入法
        /// </summary>
        None = 0x7ffff,
    }
    /// <summary>
    /// 输入法控件发送给编辑器的消息参数
    /// </summary>
    public enum A4SipMessage : uint
    {
        ClearAll = 1,
        Select = 2,
        ClipboardAlter = 3
    }
}
